package com.google.code.timetrail.gui;

import java.io.IOException;


import javax.swing.JPanel;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.GameSave;

import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoadGameFrame extends JPanel{

    private static final long serialVersionUID = 4006790632405807010L;
    private Control gameControl;
    private JPanel myCD;
    GameSave saveGame;
    JList list;
    public LoadGameFrame(Control myControl, JPanel cd) throws ClassNotFoundException, IOException {
        saveGame = new GameSave();
        this.gameControl = myControl;
        this.myCD = cd;
        setLayout(null);
        
        
        list = new JList(saveGame.listOfEntries());
        list.setBounds(121, 80, 201, 155);
        list.setVisibleRowCount(4);
        JScrollPane scrollBar = new JScrollPane(list);
        scrollBar.setBounds(77, 80, 279, 155);
        add(scrollBar);
        
        JButton btnBack = new JButton("Back");
        btnBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myCD.add(new TitleFrame(gameControl, myCD), "TakeMove");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        btnBack.setBounds(77, 266, 89, 23);
        add(btnBack);
        
        JButton btnLoad = new JButton("Load");
        btnLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Control loadedGame = new Control();
                int selectedIndex = list.getSelectedIndex();
                String[] saveFile = new String[2];
                
                try {
                    if(selectedIndex >= 0){
                        saveFile = saveGame.listOfEntries()[selectedIndex].split(" ");
                        String saveName = "";
                        String saveDate = "";
                        int count = 0;
                        for(String word: saveFile){
                            if(count < saveFile.length - 2){
                                saveName += word;
                                if(count < saveFile.length - 3){
                                    saveName += " ";
                                }
                            } else {
                                saveDate += word;
                                if(count < saveFile.length - 1){
                                    saveDate += " ";
                                }
                            }
                            count++;
                        }
                        saveFile[0] = saveName;
                        saveFile[1] = saveDate;
                    }
                    //This assumes the name is not two words
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    if(selectedIndex >= 0){
                    loadedGame = saveGame.loadGame(saveFile[0], saveFile[1]);
                    }
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if(selectedIndex >= 0){
                myCD.add(new TakingMoveFrame(loadedGame, myCD, "Loading"), "TakeMove");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
                }
            }
        });
        btnLoad.setBounds(176, 266, 89, 23);
        add(btnLoad);
        
        JButton btnNewButton = new JButton("Delete");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                int selectedIndex = list.getSelectedIndex();
                String[] saveFile = new String[2];
                try {
                    
                    
                    String saveName = "";
                    String saveDate = "";
                    if(selectedIndex >= 0){
                        saveFile = saveGame.listOfEntries()[selectedIndex].split(" ");
                        int count = 0;
                        for(String word: saveFile){
                            if(count < saveFile.length - 2){
                                saveName += word;
                                if(count < saveFile.length - 3){
                                    saveName += " ";
                                }
                            } else {
                                saveDate += word;
                                if(count < saveFile.length - 1){
                                    saveDate += " ";
                                }
                            }
                            count++;
                        }
                    }
                    saveFile[0] = saveName;
                    saveFile[1] = saveDate;
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    if(selectedIndex >= 0){
                        saveGame.deleteGame(saveFile[0], saveFile[1]);
                    }
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    myCD.add(new LoadGameFrame(gameControl, myCD), "TakeMove");
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        btnNewButton.setBounds(275, 266, 89, 23);
        add(btnNewButton);
        
        
        
        
    }
}
